package br.com.manutencaocarro.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import br.com.manutencaocarro.R;
import br.com.manutencaocarro.dao.CarroDAO;
import br.com.manutencaocarro.dao.LojaDAO;
import br.com.manutencaocarro.dao.PecaDAO;
import br.com.manutencaocarro.modelo.Loja;
import br.com.manutencaocarro.modelo.Peca;
import br.com.manutencaocarro.modelo.Carro;

public class ListagemPecasActivity extends Activity 
{
	private ListView lista;
	private Peca peca;
	private int idCarro = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_pecas);
		
		Intent intent = getIntent();
		
		if( intent.getExtras() != null )
		{
			idCarro = (int)intent.getExtras().getLong( "idCarro" );
		}

		lista = (ListView) findViewById(R.id.lista_pecas);

		lista.setOnItemClickListener( new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
				Peca pecaClicada = (Peca) adapter.getItemAtPosition(position);

				Intent irParaFormulario = new Intent( ListagemPecasActivity.this, CadastraPecaActivity.class );
				irParaFormulario.putExtra( getString(R.string.pecaselecionada), pecaClicada );
				startActivity(irParaFormulario);
			}
		});

		lista.setOnItemLongClickListener( new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) {
				peca = (Peca) adapter.getItemAtPosition(position);
				return false;
			}
		});

		registerForContextMenu(lista);
	}

	@Override
	protected void onResume() {
		super.onResume();
		carregaLista();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.listagem_pecas, menu);

		return super.onCreateOptionsMenu(menu);
	}

	private void carregaLista() 
	{
		PecaDAO dao = new PecaDAO(this);
		List<Peca> pecas;
		
		if( idCarro == -1 )
			pecas = dao.getLista();
		else
			pecas = dao.getLista(idCarro);
		
		dao.close();

		int layout = android.R.layout.simple_list_item_1;
		ArrayAdapter<Peca> adapter = new ArrayAdapter<>(this, layout , pecas);

		lista.setAdapter(adapter);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
	{
		MenuItem excluir = menu.add( R.string.excluir );

		excluir.setOnMenuItemClickListener( new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				PecaDAO dao = new PecaDAO(ListagemPecasActivity.this);
				dao.excluiPeca( peca.getId() );
				dao.close();

				carregaLista();

				Toast.makeText( ListagemPecasActivity.this, R.string.peca_excluida_com_sucesso, Toast.LENGTH_LONG ).show();

				return false;
			}
		});

		MenuItem detalhes = menu.add( R.string.ver_detalhes );

		detalhes.setOnMenuItemClickListener( new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent irParaFormulario = new Intent( ListagemPecasActivity.this, CadastraPecaActivity.class );
				Bundle params = new Bundle();
				params.putSerializable( getString(R.string.pecaselecionada), peca );
				params.putBoolean( getString(R.string.detalhar), true );
				irParaFormulario.putExtras(params);
				startActivity(irParaFormulario);

				return false;
			}
		});

		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		CarroDAO dao = new CarroDAO(this);
		List<Carro> listCarros = dao.getLista();
		dao.close();
		LojaDAO ljDao = new LojaDAO(this);
		List<Loja> listLojas = ljDao.getLista();
		ljDao.close();

		if( listCarros.size() > 0 && listLojas.size() > 0 )
		{
			int itemClicado = item.getItemId();

			switch (itemClicado) 
			{
			case R.id.cadastrar_peca :
				Intent irParaFormulario = new Intent(this, CadastraPecaActivity.class);

				startActivity(irParaFormulario);
				return false;
			default:
				return super.onOptionsItemSelected(item);
			}
		} else {
			Toast.makeText( this, R.string.cadastro_peca, Toast.LENGTH_LONG ).show();
		}

		return true;
	}
}